package com.example;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.StrJson.getString;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    @Test
    public void shuiLing() throws JSONException, IOException, SchemaException {

        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        String json = "D:\\项目数据\\data\\梁溪数据\\北大街街道\\凤凰城\\222.json";
        String jsonData = getString.readFile(json);
        JSONObject parse = JSONObject.parseObject(jsonData);
        //点
        JSONArray features = parse.getJSONArray("features");
        //点字段
        JSONObject g = (JSONObject) features.get(0);
        JSONObject p = g.getJSONObject("properties");
        StringBuilder the_geom = new StringBuilder("the_geom:MultiPoint");
        for(Map.Entry<String, Object> entry: p.entrySet()) {
            String key = entry.getKey();
            the_geom.append(",").append(key).append(":String");
        }


        File shapefileFile = new File("D:\\项目数据\\data\\梁溪数据\\北大街街道\\凤凰城\\qwe.shp");
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        Map<String, Serializable> params = new HashMap<>();
        params.put("url", shapefileFile.toURI().toURL());
        params.put("create spatial index", Boolean.TRUE);
        System.out.println(the_geom);
        SimpleFeatureType featureType = DataUtilities.createType("MultiPoint", String.valueOf(the_geom));
        ShapefileDataStore shapefileDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
        shapefileDataStore.setCharset(StandardCharsets.UTF_8);
        shapefileDataStore.createSchema(featureType);
        shapefileDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

        SimpleFeatureSource featureSource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
        ListFeatureCollection collection = new ListFeatureCollection(featureType, new ArrayList<>());
        //点数据
        for (int i = 0; i < features.size(); i++) {
            JSONObject obj = (JSONObject) features.get(i);
            JSONObject properties = obj.getJSONObject("properties");
            JSONObject geometry = obj.getJSONObject("geometry");
            JSONArray coordinates = geometry.getJSONArray("coordinates");
            // 创建一个空的点几何
            Geometry point;
            Point[] points;
            List<Point> pointList = new ArrayList<>();
            for (int j = 0; j < coordinates.size(); j++) {
                //获取经纬度坐标
                JSONArray pointArray = coordinates.getJSONArray(j);
                double lng = pointArray.getDouble(1);
                double lat = pointArray.getDouble(0);
                Point po = geometryFactory.createPoint(new Coordinate(lng, lat));
                pointList.add(po);
            }
            points = pointList.toArray(new Point[0]);
            point = geometryFactory.createMultiPoint(points);
            // 添加点几何到要素
            featureBuilder.set("the_geom", point);
            //获取字段值及字段内容
            for(Map.Entry<String, Object> entry: properties.entrySet()) {
                String key = entry.getKey();
                String value = properties.get(key).toString();
                featureBuilder.set(key,value);
            }

            // 构建并添加要素
            SimpleFeature feature = featureBuilder.buildFeature(null);
            collection.add(feature);
        }
        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) featureSource;
        Transaction transaction = new DefaultTransaction("create");
        try {
            featureStore.setTransaction(transaction);
            featureStore.addFeatures(collection);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
            shapefileDataStore.dispose();
        }

    }
}

